package cn.edu.sjtu.literature.web.controller;

import java.text.ParseException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import cn.edu.sjtu.literature.entity.Category;
import cn.edu.sjtu.literature.entity.Literature;
import cn.edu.sjtu.literature.service.exception.LiteratureAlreadyExistsException;
import cn.edu.sjtu.literature.web.util.Constants;
import cn.edu.sjtu.literature.web.view.View;

public class LiteratureSavingController extends AbstractController {

	@Override
	public View doAction(HttpServletRequest req, HttpServletResponse resp) {
		log.debug("do save literature");
		
		// add categories to view
		req.setAttribute(Constants.CATEGORIES, literatureService.getCategories());
		
		// validate and bind value
		Literature literature = validateAndBind(req);
		if (literature == null) {
			log.debug("has field error");
			return getView(Constants.VIEW_LITERATURE_FORM);
		}
		
		// save literature
		log.debug("validate field successfully and save literature");
		try {
			if (literature.getId() <= 0) {
				// new literature
				log.debug("add literature");
				literatureService.addLiterature(literature);
			} else {
				// update literature
				log.debug("update literature");
				literatureService.updateLiterature(literature);
			}
		} catch (LiteratureAlreadyExistsException e) {
			req.setAttribute(Constants.ERROR_MESSAGE_KEY, "文献刊号已存在");
			return getView(Constants.VIEW_LITERATURE_FORM);
		}
		
		req.setAttribute(Constants.SUCCESS_MESSAGE_KEY, "文献保存成功");
		return getView(Constants.VIEW_LITERATURE_MESSAGE);
	}

	private Literature validateAndBind(HttpServletRequest req) {
		String id = req.getParameter("id");
		String issueNo = req.getParameter("issueNo");
		String title = req.getParameter("title");
		String categoryId = req.getParameter("categoryId");
		String author = req.getParameter("author");
		String authorFirm = req.getParameter("authorFirm");
		String authorContactInfo = req.getParameter("authorContactInfo");
		String source = req.getParameter("source");
		String publishDate = req.getParameter("publishDate");
		
		log.debug("id: " + id);
		log.debug("issueNo: " + issueNo);
		log.debug("title: " + title);
		log.debug("categoryId: " + categoryId);
		log.debug("author: " + author);
		log.debug("authorFirm: " + authorFirm);
		log.debug("authorContactInfo: " + authorContactInfo);
		log.debug("source: " + source);
		log.debug("publishDate: " + publishDate);
		
		boolean hasFieldError = false;
		Literature literature = new Literature();
		Date now = new Date();
		if (StringUtils.isNotBlank(id) && Integer.parseInt(id) > 0) {
			// modify
			literature.setId(Integer.parseInt(id));
		} else {
			// add new
			literature.setCreated(now);
			literature.setCreatedBy(getSessionUser(req).getId());
		}
		literature.setModified(now);
		literature.setModifiedBy(getSessionUser(req).getId());
		
		if (StringUtils.isBlank(issueNo)) {
			req.setAttribute(getFieldErrorKey("issueNo"), "不能为空");
			hasFieldError = true;
		} else {
			literature.setIssueNo(issueNo);
		}
		if (StringUtils.isBlank(title)) {
			req.setAttribute(getFieldErrorKey("title"), "不能为空");
			hasFieldError = true;
		} else {
			literature.setTitle(title);
		}
		if (StringUtils.isBlank(categoryId)) {
			req.setAttribute(getFieldErrorKey("categoryId"), "请选择");
			hasFieldError = true;
		} else {
			Category category = new Category();
			category.setId(Integer.parseInt(categoryId));
			literature.setCategory(category);
		}
		if (StringUtils.isBlank(author)) {
			req.setAttribute(getFieldErrorKey("author"), "不能为空");
			hasFieldError = true;
		} else {
			literature.setAuthor(author);
		}
		if (StringUtils.isBlank(authorFirm)) {
			req.setAttribute(getFieldErrorKey("authorFirm"), "不能为空");
			hasFieldError = true;
		} else {
			literature.setAuthorFirm(authorFirm);
		}
		if (StringUtils.isBlank(authorContactInfo)) {
			req.setAttribute(getFieldErrorKey("authorContactInfo"), "不能为空");
			hasFieldError = true;
		} else {
			literature.setAuthorContactInfo(authorContactInfo);
		}
		if (StringUtils.isBlank(source)) {
			req.setAttribute(getFieldErrorKey("source"), "不能为空");
			hasFieldError = true;
		} else {
			literature.setSource(source);
		}
		if (StringUtils.isBlank(publishDate)) {
			req.setAttribute(getFieldErrorKey("publishDate"), "不能为空");
			hasFieldError = true;
		} else {
			try {
				literature.setPublishDate(Constants.NORMAL_DATE_FORMAT.parse(publishDate));
			} catch (ParseException e) {
				req.setAttribute(getFieldErrorKey("publishDate"), "格式非法");
				hasFieldError = true;
			}
		}
		req.setAttribute(Constants.LITERATURE, literature);
		return hasFieldError ? null : literature;
	}

}
